import Background from './Background'
import MapFloor from './MapFloor'
import Player from './Player'
import HP from './HP'
import Item from './Item'
import GameOver from './GameOver'


/**
 * @class
 * 游戏初始化入口
 */
export default class RunGame extends Sprite {

  constructor() {
    super();

		this.score = 0;// 分数

    this.itemPoint = new Point();// 道具二维坐标位置

    this.init();
  }

  init() {

    // 添加背景
    this.bg = new Background();
    this.addChild(this.bg);

    // 添加地板集合
    this.mapFloor = new MapFloor();
    this.addChild(this.mapFloor);

    // 飞行能量条
		this.flyEnergy = new HP(HP.HP_TYPE_ENERGY);
		this.flyEnergy.y = 7;
		this.addChild(this.flyEnergy);
		
		// 速度能量条
		this.speedEnergy = new HP(HP.HP_TYPE_SPEED);
		this.speedEnergy.y = 7;
    this.speedEnergy.x = this.flyEnergy.width + 10;
    this.speedEnergy.changeValue(-155);
		this.addChild(this.speedEnergy);

    // 玩家
    this.player = new Player(this.flyEnergy, this.speedEnergy);
    this.player.x = 180;// 初始位置
    this.player.y = 100;
    this.player.on(Player.DIE, this, this.playerDie);
    this.player.visible = false;
    this.addChild(this.player);

    // 分数
    this.scoreTxt = new Text();
		this.scoreTxt.color = '#FFFFFF';
		this.scoreTxt.fontSize = 28;
		this.scoreTxt.text = '0';
		this.scoreTxt.align = 'right';
		this.scoreTxt.x = 500;
		this.scoreTxt.y = 8;
		this.addChild(this.scoreTxt);

    // 暂停、继续
    this.pauseTxt = new Text();
    this.pauseTxt.color = '#FFFFFF';
    this.pauseTxt.fontSize = 28;
		this.pauseTxt.text = '暂停';
		this.pauseTxt.align = 'right';
		this.pauseTxt.x = 600;
    this.pauseTxt.y = 8;
    this.pauseTxt.on(Event.MOUSE_DOWN, this, this.gamePauseToggle);
		this.addChild(this.pauseTxt);

    // 结束页面
    this.gameOver = new GameOver();
    this.gameOver.visible = false;
    this.gameOver.once(Event.MOUSE_DOWN, this, this.gameReset);
		this.addChild(this.gameOver);

    Laya.timer.frameLoop(1, this, this.onLoop);
  }

  onLoop() {

    if(gamePauseHandle(this, this.onLoop)) return ;

    // 从后遍历，省去了一个长度变量，学会了
    for(let i = this.mapFloor.numChildren - 1; i > -1; i--) {
      const floor = this.mapFloor.getChildAt(i);
      if(floor.checkHit(this.player.x, this.player.y)) {// 踩到地板上了

        floor.getItems().forEach(item => {

          if(item.visible) {

            // 碰撞检测：item中心点在player内即可
            this.itemPoint.x = item.x + floor.x + Config.itemWH / 2;// correct, Config.itemWH / 2 恰好弥补了人物两边剩余空白宽度问题
            this.itemPoint.y = item.y + floor.y + Config.itemWH / 2;
            if(this.player.hitTestPoint(this.itemPoint.x, this.itemPoint.y)) {// 判断itemPoint是否再player内

							// 物品有多个类型 分类型进行判断
							if(item.type === Item.ITEM_TYPE_SPEED) {
                // item.visible = false;
                this.player.showEffect();
              } else if(item.type === Item.ITEM_TYPE_FLY)  {
                // item.visible = false;
                this.flyEnergy.changeValue(100);
							} else if(item.type === Item.ITEM_TYPE_STAR) {
								// 星星物品播放动画
                Tween.to(item, { y : -10, scaleX : 0.1, alpha : 0 }, 150, null, Handler.create(this, this.itemTweenComplete, [item]));
                this.updateScore();
							}
            }
          }
        });

        this.player.jumpReset();
        this.player.y = floor.y;

        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.onMouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onMouseUp);
      }
    }
  }

  itemTweenComplete(item) {

    // item.visible = false;// FIXME: 分数不是+1
		item.y = 0;
		item.alpha = 0.7;
		item.scale(1, 1);
	}

  updateScore() {
		this.scoreTxt.text = ++this.score;
  }

  onMouseDown() {
    this.player.jump();
  }

  onMouseUp() {
    this.player.gotoJump();
  }

  getPlayer() {
    return this.player;
  }

  playerDie() {
    Config.isOver = true;
		this.gameOver.setScore(this.score);
		this.gameOver.visible = true;
  }

  gameReset() {
    window.location.reload();
  }

  gamePauseToggle() {
    Config.isPause = !Config.isPause;
    this.pauseTxt.text = Config.isPause ? '继续' : '暂停';
  }
}